package com.mmall.controller;

import com.mmall.common.ServerResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.sound.midi.Soundbank;
import java.io.File;
import java.util.UUID;

@Controller
public class UserFileUpload {
    @RequestMapping("up.do")
    public void upload(HttpSession session, @RequestParam(value = "up_load", required = false) MultipartFile file, HttpServletRequest request) {
        if (!file.isEmpty()) {
//            System.out.println("file.isEmpty()" + file.isEmpty()); false
//            String contextPath = request.getContextPath(); /mmall
//            System.out.println("context :" + contextPath);
//            String servletPath = request.getServletPath();
//            System.out.println("servletPath :" + servletPath); /up.dp
//            String scheme = request.getScheme();
//            System.out.println("scheme :" + scheme); http
            String basePath ="E:\\mmall\\ftpfile\\img";
            //System.out.println("bashPath :"+basePath); //bashPath :C:\apache-tomcat-7.0.78\webapps\mmall\img
            String fileName = file.getOriginalFilename();
           // System.out.println("file.getOriginalFilename " + fileName);
            //UUID 可以作为保存的文件的索引，使之无重复
            String s = UUID.randomUUID().toString()+"-";
            fileName=s+fileName;
            File filePath = new File(basePath, fileName);
//            System.out.println("filePath "+filePath);
//            System.out.println(filePath.getParentFile());
            //C:\apache-tomcat-7.0.78\webapps\mmall\img
            //http://www.jquerycn.cn/a_17683 fileParentFile的用法，以及mkdirs，是只创建目录的
            //每次tomcat关闭之后C:\apache-tomcat-7.0.78\webapps\mmall\img将会消失
            if (!filePath.getParentFile().exists()) {
                System.out.println("zoule");
                filePath.getParentFile().mkdirs();
            }
            try {
                // File.separator  https://blog.csdn.net/long690276759/article/details/53393387
                //file.transferTo 提交到那个路径
                file.transferTo(filePath);
                System.out.println("basePath + File.separator + fileName"+filePath);
                System.out.println("chenggong");
            } catch (Exception e) {
                System.out.println("eroor");
            }
        } else {
            System.out.println("error");
        }

    }
}
